﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Telerik.Web.UI;

namespace PEXAM.Admin.Page_Admin
{
    public partial class DataAccessGroup : System.Web.UI.Page
    {
        string sErr = "";
        string _strPOG = "DA";
        string _db = "EXA";
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                Session["dataDAOG"] = DTO.LIST_DAOGInfo.ToDataTable();
            }

        }






        private DTO.LIST_DAInfo GetDataFromForm()
        {
            DTO.LIST_DAInfo result = new DTO.LIST_DAInfo();
            result.DAG_ID = txtRoleId.Text;
            result.NAME = txtRoleName.Text;
            result.EI = txtPassLen.Text;
            return result;
        }

        private void SetDataToForm(DTO.LIST_DAInfo objEntr)
        {
            txtRoleId.Text = objEntr.DAG_ID;
            txtRoleName.Text = objEntr.NAME;
            txtPassLen.Text = objEntr.EI;
            BUS.LIST_DAOGControl control = new BUS.LIST_DAOGControl();
            Session["dataDAOG"] = control.GetAll(objEntr.DAG_ID, ref sErr);
            dgvPOG.Rebind();
        }

        private void RefreshForm()
        {
            txtRoleId.Text = "";
            txtRoleName.Text = "";
            txtPassLen.Text = "";
            lbErr.Text = "";
            Session["dataDAOG"] = DTO.LIST_DAOGInfo.ToDataTable();
            dgvPOG.Rebind();
        }


        protected void dgvList_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            BUS.LIST_DAControl control = new BUS.LIST_DAControl();
            dgvList.DataSource = control.GetAll(ref sErr);
        }

        protected void dgvList_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName.ToUpper() == "SELECTED")
            {
                string id = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["DAG_ID"].ToString();
                BUS.LIST_DAControl ctr = new BUS.LIST_DAControl();
                SetDataToForm(ctr.Get(id, ref sErr));
                lbErr.Text = "";
            }

        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            RefreshForm();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (txtRoleId.Text == "")
            {
                lbErr.Text = "Please input Data Access Code";
                return;
            }
            DTO.LIST_DAInfo info = GetDataFromForm();
            BUS.LIST_DAControl control = new BUS.LIST_DAControl();
            if (!control.IsExist(info.DAG_ID))
            {
                control.Add(info, ref sErr);
                if (sErr == "")
                {
                    BUS.LIST_DAOGControl ctr = new BUS.LIST_DAOGControl();
                    DataTable dt = Session["dataDAOG"] as DataTable;
                    foreach (DataRow row in dt.Rows)
                    {
                        DTO.LIST_DAOGInfo infDAGO = new DTO.LIST_DAOGInfo(row);
                        infDAGO.DAG_ID = info.DAG_ID;
                        ctr.Add(infDAGO, ref sErr);
                    }
                    lbErr.Text = "Insert successfull";

                }
                else
                { lbErr.Text = sErr; }
            }
            else
            {
                sErr = control.Update(info);

                if (sErr == "")
                {
                    BUS.LIST_DAOGControl ctr = new BUS.LIST_DAOGControl();
                    ctr.Deletes(info.DAG_ID);
                    DataTable dt = Session["dataDAOG"] as DataTable;
                    foreach (DataRow row in dt.Rows)
                    {
                        DTO.LIST_DAOGInfo infDAGO = new DTO.LIST_DAOGInfo(row);
                        infDAGO.DAG_ID = info.DAG_ID;
                        ctr.Add(infDAGO, ref sErr);
                    }
                    lbErr.Text = "Update successfull";
                }
                else
                { lbErr.Text = sErr; }
            }
            dgvList.Rebind();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            foreach (GridItem item in dgvList.SelectedItems)
            {
                if (item.ItemIndex >= 0)
                {
                    string id = item.OwnerTableView.DataKeyValues[item.ItemIndex]["DAG_ID"].ToString();
                    BUS.LIST_DAControl ctr = new BUS.LIST_DAControl();
                    sErr = ctr.Delete(id);
                    BUS.LIST_DAOGControl popCtr = new BUS.LIST_DAOGControl();
                    sErr += popCtr.Deletes(id);
                }
            }
            dgvList.Rebind();
        }
        protected void cboPOG_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
        {
            BUS.POGControl ctr = new BUS.POGControl();
            DataTable dt = ctr.GetAll(ref sErr);
            RadComboBox comboBox = (RadComboBox)sender;
            // Clear the default Item that has been re-created from ViewState at this point.
            comboBox.Items.Clear();

            foreach (DataRow row in dt.Rows)
            {
                RadComboBoxItem item = new RadComboBoxItem();
                item.Text = row["ROLE_ID"].ToString().Trim();
                item.Value = row["ROLE_ID"].ToString().Trim();
                item.Attributes.Add("ROLE_ID", row["ROLE_ID"].ToString().Trim());

                comboBox.Items.Add(item);

                item.DataBind();
            }

        }

        protected void dgvPOG_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            dgvPOG.DataSource = Session["dataDAOG"] as DataTable;
        }

        protected void dgvPOG_ItemCommand(object sender, GridCommandEventArgs e)
        {

        }

        protected void dgvPOG_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            //Set new values
            Hashtable newValues = new Hashtable();
            //The GridTableView will fill the values from all editable columns in the hash
            //e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
            string id = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ROLE_ID"].ToString().Trim();

            DataTable dt = (DataTable)Session["dataDAOG"];
            string keySearch = "ROLE_ID='" + id + "'";
            DataRow[] rows = dt.Select(keySearch);

            //foreach (DictionaryEntry entry in newValues)
            //{

            //    rows[0][(string)entry.Key] = entry.Value;
            //}
            rows[0]["ROLE_ID"] = Session["ROLE_ID"];
            rows[0]["DAG_ID"] = txtRoleId.Text;
            Session["dataDAOG"] = dt;
            dgvList.Rebind();
        }

        protected void dgvPOG_DeleteCommand(object sender, GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            //Set new values
            Hashtable newValues = new Hashtable();
            //The GridTableView will fill the values from all editable columns in the hash
            e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
            string id = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ROLE_ID"].ToString().Trim();

            DataTable dt = (DataTable)Session["dataDAOG"];
            string keySearch = "ROLE_ID='" + id + "'";
            DataRow[] rows = dt.Select(keySearch);
            dt.Rows.Remove(rows[0]);
            Session["dataDAOG"] = dt;
            dgvList.Rebind();
        }

        protected void dgvPOG_InsertCommand(object sender, GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            //Set new values
            Hashtable newValues = new Hashtable();
            //The GridTableView will fill the values from all editable columns in the hash
            //e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);

            DataTable dt = (DataTable)Session["dataDAOG"];
            DataRow row = dt.NewRow();

            //foreach (DictionaryEntry entry in newValues)
            //{
            //    row[(string)entry.Key] = entry.Value;
            //}
            row["DAG_ID"] = txtRoleId.Text;
            row["ROLE_ID"] = Session["ROLE_ID"];
            dt.Rows.Add(row);
            Session["dataDAOG"] = dt;
            dgvList.Rebind();
        }

        protected void dgvPOG_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item.IsInEditMode)
            {
                GridEditableItem item = (GridEditableItem)e.Item;

                if (!(e.Item is IGridInsertItem))
                {

                    RadComboBox comboBox = (RadComboBox)e.Item.FindControl("RadComboBox2");
                    if (comboBox != null)
                    {
                        BUS.POGControl ctr = new BUS.POGControl();
                        DataTable dt = ctr.GetAll(ref sErr);
                        // Clear the default Item that has been re-created from ViewState at this point.
                        comboBox.Items.Clear();

                        foreach (DataRow row in dt.Rows)
                        {
                            RadComboBoxItem selectedItem = new RadComboBoxItem();
                            selectedItem.Text = row["ROLE_ID"].ToString().Trim();
                            selectedItem.Value = row["ROLE_ID"].ToString().Trim();
                            selectedItem.Attributes.Add("ROLE_ID", row["ROLE_ID"].ToString().Trim());

                            comboBox.Items.Add(selectedItem);

                            selectedItem.DataBind();
                            Session["ROLE_ID"] = selectedItem.Value;

                        }
                    }
                }
            }

        }
        protected void OnSelectedIndexChangedHandler(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            Session["ROLE_ID"] = e.Value;
        }

    }
}
